<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="1000px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="标题" prop="title">
                <el-input v-model.trim="form.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="来源">
                <el-input v-model.trim="form.source"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="作者" prop="author">
                <el-input v-model.trim="form.author"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发布时间" prop="pudDate">
                <el-date-picker
                  v-model="form.pudDate"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序">
                <el-input-number
                  v-model="form.orderBy"
                  :min="1"
                  :max="1000"
                  size="medium"
                  label="排序"
                ></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="内容" prop="content">
                <UEditor
                  ref="ueditor"
                  :ueHeight="300"
                  :ueWidth="880"
                  :defaultMsg="form.content"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import moment from "moment";
import { sysPolicyAdd, sysPolicyUpdate } from "@/api/admin/system/sysPolicy.js";
import Bus from "@/utils/bus.js";
export default {
  data() {
    return {
      title: "新增",
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        title: "",
        source: "",
        content: "",
        author: "",
        pudDate: "",
        orderBy: "10",
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
        author: [{ required: true, message: "请输入作者", trigger: "blur" }],
        pudDate: [
          { required: true, message: "请选择发布时间", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    Bus.$on("showDialogPolicyAdd", (v) => {
      if (v) {
        this.checkedData = v;
        this.handleAddOrEdit(2);
      } else {
        this.handleAddOrEdit(1);
      }
    });
  },
  beforeDestroy() {
    Bus.$off("showDialogPolicyAdd");
  },
  methods: {
    saveFile(v) {
      this.form.fileId = v[0].id;
    },
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = "编辑";
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || "";
          }
        }
        this.form.id = this.checkedData.id;
      } else {
        this.title = "新增";
        this.form.author = this.$store.getters.username;
        this.form.pudDate = new Date();
        delete this.form.id;
      }
      this.addOrEdit = type;
      this.dialogVisible = true;
    },
    submitForm(form) {
      this.form.pudDate = moment(this.form.pudDate).format(
        "YYYY-MM-DD HH:mm:ss"
      );
      this.form.content = this.$refs.ueditor.message;
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit();
        }
      });
    },
    //新增
    add() {
      sysPolicyAdd(this.form).then((res) => {
        this.dialogVisible = false;
        Bus.$emit("refreshPolicyList");
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
    //编辑
    edit() {
      sysPolicyUpdate(this.form).then((res) => {
        this.dialogVisible = false;
        Bus.$emit("refreshPolicyList");
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields();
      for (let key in this.form) {
        this.form[key] = "";
        this.form.orderBy = "10"
      }
    },
  },
};
</script>